# -*- encoding:utf-8 -*-

from service.cubeinfo.CubeRankCrawler import CubeRankCrawler
import time
from service.cubeinfo.CubeNameCrawler import CubeNameCrawler
from service.cubeinfo.CubeStockCrawler import CubeStockCrawler

__author__ = 'shudong.msd'


def writeToDBWithRank():
    print u'开始 cubeinfo 组合排行榜 抓取'

    crc = CubeRankCrawler()

    # 最热
    crc.init_target('http://xueqiu.com/cubes/discover/rank/cube/list.json?category=14')
    count1 = crc.writeToDB_rank(0)
    time.sleep(0.5)
    print u'最热排行榜 ok! 抓取记录数：'+str(count1)

    # 收益高 月
    crc.init_target('http://xueqiu.com/cubes/discover/rank/cube/list.json?category=12&market=cn&profit=monthly_gain')
    count2 = crc.writeToDB_rank(111)
    time.sleep(0.5)
    print u'收益月排行榜 ok! 抓取记录数：'+str(count2)

    # 涨跌快 112 月
    crc.init_target(
        'http://xueqiu.com/cubes/discover/rank/cube/list.json?market=cn&sale_flag=0&stock_positions=0&sort=grow_fast&category=12&profit=monthly_gain')
    count3 = crc.writeToDB_rank(112)
    time.sleep(0.5)
    print u'涨跌速度月排行榜 ok! 抓取记录数：'+str(count3)

    # 收益高 121 日
    crc.init_target(
        'http://xueqiu.com/cubes/discover/rank/cube/list.json?market=cn&sale_flag=0&stock_positions=0&sort=best_benefit&category=12&profit=daily_gain')
    count4 = crc.writeToDB_rank(121)
    time.sleep(0.5)
    print u'收益日排行榜 ok! 抓取记录数：'+str(count4)

    # 涨跌快 122 日
    crc.init_target(
        'http://xueqiu.com/cubes/discover/rank/cube/list.json?market=cn&sale_flag=0&stock_positions=0&sort=grow_fast&category=12&profit=daily_gain')
    count5 = crc.writeToDB_rank(122)
    time.sleep(0.5)
    print u'涨跌速度日排行榜 ok! 抓取记录数：'+str(count5)

    # 最人气 2
    crc.init_target('http://xueqiu.com/cubes/discover/rank/cube/list.json?category=10&market=cn')
    count6 = crc.writeToDB_rank(2)
    print u'最人气排行榜 ok! 抓取记录数：'+str(count6)

    print 'ok!!!'

    return count1+count2+count3+count4+count5+count6


def writeToDBWithInfo():
    print u'开始 cubeinfo 组合详情 抓取'

    crc = CubeRankCrawler()

    cube_list = list()

    filter_symbol = []

    # 最热
    crc.init_target('http://xueqiu.com/cubes/discover/rank/cube/list.json?category=14')
    cube_list.extend(crc.get_cube_list())
    time.sleep(0.5)

    tmp_list = list()

    for row in cube_list:
        if row['symbol'] not in filter_symbol:
            filter_symbol.append(row['symbol'])
            tmp_list.append(row)

    cube_list = tmp_list

    # 收益高 月
    crc.init_target('http://xueqiu.com/cubes/discover/rank/cube/list.json?category=12&market=cn&profit=monthly_gain')
    for row in crc.get_cube_list():
        if row['symbol'] in filter_symbol:
            continue
        filter_symbol.append(row['symbol'])
        cube_list.append(row)
    time.sleep(0.5)

    # 涨跌快 112 月
    crc.init_target(
        'http://xueqiu.com/cubes/discover/rank/cube/list.json?market=cn&sale_flag=0&stock_positions=0&sort=grow_fast&category=12&profit=monthly_gain')
    for row in crc.get_cube_list():
        if row['symbol'] in filter_symbol:
            continue
        filter_symbol.append(row['symbol'])
        cube_list.append(row)
    time.sleep(0.5)

    # 收益高 121 日
    crc.init_target(
        'http://xueqiu.com/cubes/discover/rank/cube/list.json?market=cn&sale_flag=0&stock_positions=0&sort=best_benefit&category=12&profit=daily_gain')
    for row in crc.get_cube_list():
        if row['symbol'] in filter_symbol:
            continue
        filter_symbol.append(row['symbol'])
        cube_list.append(row)
    time.sleep(0.5)

    # 涨跌快 122 日
    crc.init_target(
        'http://xueqiu.com/cubes/discover/rank/cube/list.json?market=cn&sale_flag=0&stock_positions=0&sort=grow_fast&category=12&profit=daily_gain')
    for row in crc.get_cube_list():
        if row['symbol'] in filter_symbol:
            continue
        filter_symbol.append(row['symbol'])
        cube_list.append(row)
    time.sleep(0.5)

    # 最人气 2
    crc.init_target('http://xueqiu.com/cubes/discover/rank/cube/list.json?category=10&market=cn')
    for row in crc.get_cube_list():
        if row['symbol'] in filter_symbol:
            continue
        filter_symbol.append(row['symbol'])
        cube_list.append(row)

    crc.set_cube_list(cube_list)

    count = crc.writeToDB_info()

    print 'ok!!!'

    return count


def updateDBWithRank():
    print u'开始 cubeinfo 组合排行榜 更新'

    crc = CubeRankCrawler()

    # 最热
    crc.init_target('http://xueqiu.com/cubes/discover/rank/cube/list.json?category=14')
    count1 = crc.updateDB_rank(0)
    time.sleep(0.5)
    print u'最热排行榜 ok! 更新记录数：'+str(count1)

    # 收益高 月
    crc.init_target('http://xueqiu.com/cubes/discover/rank/cube/list.json?category=12&market=cn&profit=monthly_gain')
    count2 = crc.updateDB_rank(111)
    time.sleep(0.5)
    print u'收益月排行榜 ok! 更新记录数：'+str(count2)

    # 涨跌快 112 月
    crc.init_target(
        'http://xueqiu.com/cubes/discover/rank/cube/list.json?market=cn&sale_flag=0&stock_positions=0&sort=grow_fast&category=12&profit=monthly_gain')
    count3 = crc.updateDB_rank(112)
    time.sleep(0.5)
    print u'涨跌速度月排行榜 ok! 更新记录数：'+str(count3)

    # 收益高 121 日
    crc.init_target(
        'http://xueqiu.com/cubes/discover/rank/cube/list.json?market=cn&sale_flag=0&stock_positions=0&sort=best_benefit&category=12&profit=daily_gain')
    count4 = crc.updateDB_rank(121)
    time.sleep(0.5)
    print u'收益日排行榜 ok! 更新记录数：'+str(count4)

    # 涨跌快 122 日
    crc.init_target(
        'http://xueqiu.com/cubes/discover/rank/cube/list.json?market=cn&sale_flag=0&stock_positions=0&sort=grow_fast&category=12&profit=daily_gain')
    count5 = crc.updateDB_rank(122)
    time.sleep(0.5)
    print u'涨跌速度排行榜 ok! 更新记录数：'+str(count5)

    # 最人气 2
    crc.init_target('http://xueqiu.com/cubes/discover/rank/cube/list.json?category=10&market=cn')
    count6 = crc.updateDB_rank(2)
    print u'人气排行榜 ok! 更新记录数：'+str(count6)

    print 'ok!!!'

    return count1+count2+count3+count4+count5+count6


def updateDBWithInfo():
    print u'开始 cubeinfo 组合详情 更新'

    crc = CubeRankCrawler()

    cube_list = list()

    filter_symbol = []

    # 最热
    crc.init_target('http://xueqiu.com/cubes/discover/rank/cube/list.json?category=14')
    cube_list.extend(crc.get_cube_list())
    time.sleep(0.5)

    for row in cube_list:
        filter_symbol.append(row['symbol'])

    # 收益高 月
    crc.init_target('http://xueqiu.com/cubes/discover/rank/cube/list.json?category=12&market=cn&profit=monthly_gain')
    for row in crc.get_cube_list():
        if row['symbol'] in filter_symbol:
            continue
        filter_symbol.append(row['symbol'])
        cube_list.append(row)
    time.sleep(0.5)

    # 涨跌快 112 月
    crc.init_target(
        'http://xueqiu.com/cubes/discover/rank/cube/list.json?market=cn&sale_flag=0&stock_positions=0&sort=grow_fast&category=12&profit=monthly_gain')
    for row in crc.get_cube_list():
        if row['symbol'] in filter_symbol:
            continue
        filter_symbol.append(row['symbol'])
        cube_list.append(row)
    time.sleep(0.5)

    # 收益高 121 日
    crc.init_target(
        'http://xueqiu.com/cubes/discover/rank/cube/list.json?market=cn&sale_flag=0&stock_positions=0&sort=best_benefit&category=12&profit=daily_gain')
    for row in crc.get_cube_list():
        if row['symbol'] in filter_symbol:
            continue
        filter_symbol.append(row['symbol'])
        cube_list.append(row)
    time.sleep(0.5)

    # 涨跌快 122 日
    crc.init_target(
        'http://xueqiu.com/cubes/discover/rank/cube/list.json?market=cn&sale_flag=0&stock_positions=0&sort=grow_fast&category=12&profit=daily_gain')
    for row in crc.get_cube_list():
        if row['symbol'] in filter_symbol:
            continue
        filter_symbol.append(row['symbol'])
        cube_list.append(row)
    time.sleep(0.5)

    # 最人气 2
    crc.init_target('http://xueqiu.com/cubes/discover/rank/cube/list.json?category=10&market=cn')
    for row in crc.get_cube_list():
        if row['symbol'] in filter_symbol:
            continue
        filter_symbol.append(row['symbol'])
        cube_list.append(row)

    crc.set_cube_list(cube_list)

    count = crc.updateDB_info()

    print 'ok!!!'

    return count

# 写入组合名称
def write_cube_name():
    print u'开始 cubeinfo 组合名称 抓取'

    crc = CubeNameCrawler()

    cube_list = list()

    filter_symbol = []

    # 最热
    crc.init_target('http://xueqiu.com/cubes/discover/rank/cube/list.json?category=14')
    cube_list.extend(crc.get_cube_list())
    time.sleep(0.5)

    tmp_list = list()

    for row in cube_list:
        if row['symbol'] not in filter_symbol:
            filter_symbol.append(row['symbol'])
            tmp_list.append(row)

    cube_list = tmp_list

    # 收益高 月
    crc.init_target('http://xueqiu.com/cubes/discover/rank/cube/list.json?category=12&market=cn&profit=monthly_gain')
    for row in crc.get_cube_list():
        if row['symbol'] in filter_symbol:
            continue
        filter_symbol.append(row['symbol'])
        cube_list.append(row)
    time.sleep(0.5)

    # 涨跌快 112 月
    crc.init_target(
        'http://xueqiu.com/cubes/discover/rank/cube/list.json?market=cn&sale_flag=0&stock_positions=0&sort=grow_fast&category=12&profit=monthly_gain')
    for row in crc.get_cube_list():
        if row['symbol'] in filter_symbol:
            continue
        filter_symbol.append(row['symbol'])
        cube_list.append(row)
    time.sleep(0.5)

    # 收益高 121 日
    crc.init_target(
        'http://xueqiu.com/cubes/discover/rank/cube/list.json?market=cn&sale_flag=0&stock_positions=0&sort=best_benefit&category=12&profit=daily_gain')
    for row in crc.get_cube_list():
        if row['symbol'] in filter_symbol:
            continue
        filter_symbol.append(row['symbol'])
        cube_list.append(row)
    time.sleep(0.5)

    # 涨跌快 122 日
    crc.init_target(
        'http://xueqiu.com/cubes/discover/rank/cube/list.json?market=cn&sale_flag=0&stock_positions=0&sort=grow_fast&category=12&profit=daily_gain')
    for row in crc.get_cube_list():
        if row['symbol'] in filter_symbol:
            continue
        filter_symbol.append(row['symbol'])
        cube_list.append(row)
    time.sleep(0.5)

    # 最人气 2
    crc.init_target('http://xueqiu.com/cubes/discover/rank/cube/list.json?category=10&market=cn')
    for row in crc.get_cube_list():
        if row['symbol'] in filter_symbol:
            continue
        filter_symbol.append(row['symbol'])
        cube_list.append(row)

    crc.set_cube_list(cube_list)

    count = crc.writeToDB()

    print 'ok!!!'

    return count

# 更新组合名称
def update_cube_name():
    print u'开始更新组合名称...'

    crc = CubeNameCrawler()

    cube_list = list()

    filter_symbol = []

    # 最热
    crc.init_target('http://xueqiu.com/cubes/discover/rank/cube/list.json?category=14')
    cube_list.extend(crc.get_cube_list())
    time.sleep(0.5)

    for row in cube_list:
        filter_symbol.append(row['symbol'])

    # 收益高 月
    crc.init_target('http://xueqiu.com/cubes/discover/rank/cube/list.json?category=12&market=cn&profit=monthly_gain')
    for row in crc.get_cube_list():
        if row['symbol'] in filter_symbol:
            continue
        filter_symbol.append(row['symbol'])
        cube_list.append(row)
    time.sleep(0.5)

    # 涨跌快 112 月
    crc.init_target(
        'http://xueqiu.com/cubes/discover/rank/cube/list.json?market=cn&sale_flag=0&stock_positions=0&sort=grow_fast&category=12&profit=monthly_gain')
    for row in crc.get_cube_list():
        if row['symbol'] in filter_symbol:
            continue
        filter_symbol.append(row['symbol'])
        cube_list.append(row)
    time.sleep(0.5)

    # 收益高 121 日
    crc.init_target(
        'http://xueqiu.com/cubes/discover/rank/cube/list.json?market=cn&sale_flag=0&stock_positions=0&sort=best_benefit&category=12&profit=daily_gain')
    for row in crc.get_cube_list():
        if row['symbol'] in filter_symbol:
            continue
        filter_symbol.append(row['symbol'])
        cube_list.append(row)
    time.sleep(0.5)

    # 涨跌快 122 日
    crc.init_target(
        'http://xueqiu.com/cubes/discover/rank/cube/list.json?market=cn&sale_flag=0&stock_positions=0&sort=grow_fast&category=12&profit=daily_gain')
    for row in crc.get_cube_list():
        if row['symbol'] in filter_symbol:
            continue
        filter_symbol.append(row['symbol'])
        cube_list.append(row)
    time.sleep(0.5)

    # 最人气 2
    crc.init_target('http://xueqiu.com/cubes/discover/rank/cube/list.json?category=10&market=cn')
    for row in crc.get_cube_list():
        if row['symbol'] in filter_symbol:
            continue
        filter_symbol.append(row['symbol'])
        cube_list.append(row)

    crc.set_cube_list(cube_list)

    count = crc.updateDB()

    print 'ok!!!'

    return count


#写入组合中的股票组合信息
def writeStockGroupForCube():
    print u'开始 cubeinfo 组合中股票详情 抓取'

    c = CubeStockCrawler()
    c.set_target('http://xueqiu.com/p/')
    count = c.writeToDB()
    print 'ok!!!'
    return count


# 测试访问情况
def test_get_page():
    crawler = CubeRankCrawler()
    crawler.init_target('http://xueqiu.com/cubes/discover/rank/cube/list.json?category=10&market=cn')
    print crawler.get_cube_list()

